package com.lianliankan.llkan;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.example.llkan.R;
import com.lianliankan.util.ButtonSelector;
import com.lianliankan.util.PlaySoundPool;
import com.lianliankan.widget.DialogView;

public class GameActivity extends Activity {
	private ImageButton pauseButton;
	private int barRate = 100;
	private final int RATETIME = 600;// 1秒为10
	private Boolean isOpen = false;
	private ProgressBar bar;
	private DialogView dialog;
	private ImageView readyImg;
	private RelativeLayout gameLayout;
	private PlaySoundPool playSoundPool;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);
		findView();
		init();
	}

	private void findView() {
		bar = (ProgressBar) findViewById(R.id.bar);
		readyImg = (ImageView) findViewById(R.id.gameImage);
		gameLayout = (RelativeLayout) findViewById(R.id.gameLayout);
		pauseButton = (ImageButton) findViewById(R.id.pauseButton);
	}

	private void init() {
		ButtonSelector bSelector = new ButtonSelector();

		pauseButton.setBackgroundDrawable(bSelector.newSelector(
				GameActivity.this, R.drawable.pausebtn, R.drawable.pausepress,
				R.drawable.pausepress, R.drawable.pausepress));
		pauseButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 生成一个请求对象
				gameHandler.removeCallbacks(gameStartThread);
				dialog = new DialogView(GameActivity.this);
				dialog.getContinueIMB().setOnClickListener(
						new OnClickListener() {
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								dialog.dismiss();
								gameHandler.post(gameStartThread);
							}
						});
				dialog.getExitIMB().setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(GameActivity.this,
								RankActivity.class);
						startActivity(intent);
						System.exit(0);
					}
				});
				dialog.show();
			}
		});

		playSoundPool = new PlaySoundPool(GameActivity.this);
		playSoundPool.loadSfx(R.raw.readygo, 1);
		playSoundPool.play(1, 0);

		openAnimation(1, 0.6, R.drawable.r1);
		gameHandler.postDelayed(openThred, 1000);
	}

	Handler gameHandler = new Handler();
	Runnable gameStartThread = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub

			bar.setProgress(barRate);
			barRate = barRate - 1;
			if (barRate < 0) {
				gameHandler.removeCallbacks(gameStartThread);
				Intent intent = new Intent(GameActivity.this,
						SettlementActivity.class);
				intent.putExtra("score", GameView.score);
				startActivity(intent);
				finish();
			} else {
				gameHandler.postDelayed(gameStartThread, RATETIME);
			}
		}
	};
	Runnable openThred = new Runnable() {

		@Override
		public void run() {
			if (isOpen) {
				playSoundPool.release();
				gameLayout.removeView(readyImg);
				gameHandler.post(gameStartThread);
				gameHandler.removeCallbacks(openThred);
			} else if (!isOpen) {
				openAnimation(0.8, 0.8, R.drawable.r2);
				isOpen = true;
				gameHandler.postDelayed(openThred, 800);
			}
		}
	};

	private void openAnimation(double stime, double atime, int imgId) {
		AnimationSet animationSet = new AnimationSet(false);
		ScaleAnimation scaleAnimation = new ScaleAnimation(0.1f, 1, 0.1f, 1,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scaleAnimation.setDuration((int) (stime * 1000));
		animationSet.addAnimation(scaleAnimation);
		animationSet.setFillAfter(true);
		animationSet.setFillBefore(false);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setDuration((int) (atime * 1000));
		animationSet.addAnimation(alphaAnimation);
		readyImg.setImageResource(imgId);
		readyImg.startAnimation(animationSet);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog isExit = new AlertDialog.Builder(this).create();
			isExit.setTitle("系统提示");
			isExit.setMessage("确定要退出吗");
			isExit.setButton("确定", listener);
			isExit.setButton2("取消", listener);
			isExit.show();
		}
		return false;
	}

	DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
				Intent intent = new Intent(GameActivity.this,
						RankActivity.class);
				startActivity(intent);
				finish();
				break;
			case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
				break;
			default:
				break;
			}
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.welcome, menu);
		return true;
	}

	protected void onResume() {
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onResume();
	}
}
